package com.action.log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.aspectj.util.FileUtil;
import org.json.JSONObject;

import antlr.StringUtils;

import com.enetiy.Area;
import com.enetiy.Userlog;
import com.enetiy.Users;
import com.opensymphony.xwork2.ActionContext;
import com.service.AreaService;
import com.service.LoginService;
import com.service.UserlogService;
import com.util.FileNameUtil;
import com.util.IPUtil;
import com.util.Verify;

public class UserLogAction {
	
	private int userid;
	private int areaid;
	private File[] file;
	private String[] fileFileName;
	private String  deviceType;
	private String phone;
	
	
	private File file1;
	private File file2;
	private String file1FileName;
	private String file2FileName;
	
	private UserlogService userlogService;
	private LoginService loginService;
	
	private String logfileStr;
	
	
	
	/**
	 * 客户端日志上传  >Nf051
	 */
	public void uploadLog(){
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		boolean flg = false;
		try {
			out = response.getWriter();
			if(file1!=null){
				File [] f ={file1,file2};
				String [] s = {file1FileName,file2FileName};
				setFile(f);
				setFileFileName(s);
			}
			if(file!=null){
					String d = "/log"+areaid+"-"+deviceType+"-"+userid+"-"+phone;
					//文件夹路劲
					String path = IPUtil.getFileLogPath() +"/userlog"+d;
					path = new String(path.getBytes("iso8859-1"),"UTF-8");
					//创建文件夹
					File logfile1 =new File(path);
					if(!logfile1.exists()){
						logfile1.mkdirs();
					}
					System.out.println("文件夹:"+path);
					
					for (int i = 0; i < file.length; i++) {
						if(file[i]!=null){
							String pathfile=path+"/"+FileNameUtil.imageupload(fileFileName[i], FileNameUtil.fileName()) ;
							System.out.println("文件名:"+pathfile);
							File logfile =new File(pathfile);
							if(logfile.createNewFile()){
								FileUtil.copyFile(file[i], logfile);
								flg = true;
							}
						}
						
					}
			}
			if(flg)
				json.put("code", 1);
			else
				json.put("code", -1);
		} catch (Exception e) {
			json.put("code", -1);
			e.printStackTrace();
		}
		out.print(json);
	}
	
	
	/**
	 * 
	 * 网页查看日志文件夹
	 */
	public String selectLogList(){
		if( phone==null)
			phone="";
		phone = phone.replaceAll("/","-");
		String path = IPUtil.getFileLogPath() +"/userlog";
		File logfile =new File(path);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		
		List<String> fileStr = new ArrayList<String>();
		String[] loglist = logfile.list();
		for (int i = 0; i < loglist.length; i++) {
			File f =new File(path+"/"+loglist[i]);
			cal.setTimeInMillis(f.lastModified());
			String format = sdf.format(cal.getTime());
			if(loglist[i].contains(phone)||format.contains(phone)){
				format=format+loglist[i];
				System.out.println(format);
				fileStr.add(format);
			}
		}
		
		Map<String, Object> request = ActionContext.getContext().getContextMap();
		request.put("fileStr", fileStr);
		return "selectLogList";
	}
	
	/**
	 * 
	 * 网页查看日志
	 */
	public String selectFileList(){
		System.out.println("selectFileList--"+logfileStr);
		boolean flg = false;
		if(logfileStr!=null&&!logfileStr.equals("")){
			String logfilename = logfileStr.substring(1);
			String path = IPUtil.getFileLogPath() +"/userlog/"+logfilename;
			File file = new File(path);
			String[] logfilelist=null;
			if(file.exists()){
				File logfileone =new File(path);
				logfilelist = logfileone.list();
				Map<String, Object> request = ActionContext.getContext().getContextMap();
				request.put("logfilelist", logfilelist);
				request.put("folder", logfilename);
			}
		}
		return "selectFileList";
	}
	
	
	public void downLogfile(){
		 System.out.println("logfileStr---"+logfileStr);
		try {
			if(logfileStr!=null&&!logfileStr.equals("")){
				String path = IPUtil.getFileLogPath() +"/userlog/"+logfileStr;
				File file = new File(path);
				logfileStr=logfileStr.substring(logfileStr.lastIndexOf("/"));	
					if(file.exists()){
						HttpServletResponse response = ServletActionContext.getResponse();
						//创建输入流
						FileInputStream fis;
							fis = new FileInputStream(file);
						//输出流
						ServletOutputStream os = response.getOutputStream();
						//设置文件的字符集
						logfileStr = URLEncoder.encode(logfileStr, "utf-8");
						//设置下载方式，"content-disposition"为附件方式下载，"attachment;filename="为文件下载完后的默认名
						response.setHeader("content-disposition", "attachment;filename="+logfileStr);
						//输入输出流
						byte[] b=new byte[1024];
						int len=-1;
						while((len=fis.read(b))!=-1){
							os.write(b, 0, len);
						}
						os.flush();
						os.close();
						fis.close();
					}
				}
		
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}		
			
		
	


	public void setUserid(int userid) {
		this.userid = userid;
	}
	public void setAreaid(int areaid) {
		this.areaid = areaid;
	}
	public void setFile(File[] file) {
		this.file = file;
	}
	public void setFileFileName(String[] fileFileName) {
		this.fileFileName = fileFileName;
	}
	public void setUserlogService(UserlogService userlogService) {
		this.userlogService = userlogService;
	}
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
	public void setFile1(File file1) {
		this.file1 = file1;
	}
	public void setFile2(File file2) {
		this.file2 = file2;
	}
	public void setFile1FileName(String file1FileName) {
		this.file1FileName = file1FileName;
	}
	public void setFile2FileName(String file2FileName) {
		this.file2FileName = file2FileName;
	}


	public void setDeviceType(String deviceType) {
		this.deviceType = deviceType;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}


	public void setLogfileStr(String logfileStr) {
		this.logfileStr = logfileStr;
	}

	
	
}
